package com.waoqi.docgen;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.Data;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;

import java.util.List;
import java.util.Map;

/**
 * @author yi.jian
 * @since 2020-03-10
 */
@Data
public class PostmanTwoEntity {
    // static Map<String, Object> map = Maps.newHashMap();
    // static Map<String, Object> requestsMap = Maps.newHashMap();
    // static List<Map<String, Object>> requestsMapList =  Lists.newLinkedList();
    // static Map<String, Object> queryParamsMap = Maps.newHashMap();
    // static List<Map<String, Object>> queryParamsMapList = Lists.newLinkedList();
    // static Map<String, Object> dataMap = Maps.newHashMap();
    // static List<Map<String, Object>> dataMapList =  Lists.newLinkedList();
    // static Map<String, Object> foldersMap = Maps.newHashMap();
    // static List<Map<String, Object>> foldersMapList = Lists.newLinkedList();

    static String collection_id = ApiDocTwoMain.getPostmanId();


    //项目设置
    public static  Map<String, Object> getMapMap(){
        //folders   requests  folders_order
        Map<String, Object> requestsMapTmp = Maps.newHashMap();
        requestsMapTmp.put("id", collection_id);
//        requestsMapTmp.put("name", ApiDocMain.projectName);
        requestsMapTmp.put("description", "项目postman请求测试");
        requestsMapTmp.put("order", Lists.newLinkedList());
        requestsMapTmp.put("timestamp", System.currentTimeMillis());
        requestsMapTmp.put("owner", 0);
        requestsMapTmp.put("public", false);
        return requestsMapTmp;
    }


    //文件夹设置
    public static  Map<String, Object> getFoldersMap(){
        //name  description collectionId order
        Map<String, Object> requestsMapTmp = Maps.newHashMap();
        requestsMapTmp.put("id", ApiDocTwoMain.getPostmanId());
        requestsMapTmp.put("name", "商品");
        requestsMapTmp.put("description", "");
        requestsMapTmp.put("folders_order", Lists.newLinkedList());
        requestsMapTmp.put("owner", 0);
        requestsMapTmp.put("collectionId", collection_id);
        return requestsMapTmp;
    }


    //文件夹中的接口
    public static  Map<String, Object> getRequestsMap(){
        // url  queryParams method   data   name description folder
        Map<String, Object> requestsMapTmp = Maps.newHashMap();
        requestsMapTmp.put("id", ApiDocTwoMain.getPostmanId() );
        // requestsMapTmp.put("headers", "Content-Type: application/json");
        requestsMapTmp.put("headerData", Lists.newLinkedList());
        requestsMapTmp.put("preRequestScript", null);
        requestsMapTmp.put("pathVariables", null);
        requestsMapTmp.put("pathVariableData", Lists.newLinkedList());
        requestsMapTmp.put("dataMode", "params");
        requestsMapTmp.put("tests", null);
        requestsMapTmp.put("currentHelper", "normal");
        requestsMapTmp.put("helperAttributes", null);
        requestsMapTmp.put("time", System.currentTimeMillis());
        requestsMapTmp.put("description", "");
        requestsMapTmp.put("collection_id", collection_id);
        requestsMapTmp.put("collectionId", collection_id);
        requestsMapTmp.put("responses", Lists.newLinkedList());
        requestsMapTmp.put("rawModeData", "");
        return requestsMapTmp;
    }


    //接口中的请求参数（GET）
    public static  Map<String, Object> getQueryParams(){
        //key  value   description
        Map<String, Object> requestsMapTmp = Maps.newHashMap();
        requestsMapTmp.put("enabled", true);
        requestsMapTmp.put("equals", true);
        return requestsMapTmp;
    }

    //接口中的请求参数（post）
    public static  Map<String, Object> getDataMap(){
        //key  value   description
        Map<String, Object> requestsMapTmp = Maps.newHashMap();
        requestsMapTmp.put("type", "text");
        requestsMapTmp.put("enabled", true);
        return requestsMapTmp;
    }

/*

    static {


        map.put("id", collection_id);
        map.put("name", "菜品项目");
        map.put("description", "项目postman请求测试");
        map.put("order", Lists.newLinkedList());
        map.put("folders", foldersMapList);
        map.put("folders_order", getFolderOrder(foldersMapList));//
        map.put("timestamp", System.currentTimeMillis());
        map.put("owner", 0);
        map.put("public", false);
        map.put("requests", requestsMapList);


        requestsMap.put("id", "43a1ffdf-cfe5-9c68-5a65-c79a2363e429");
        requestsMap.put("headers", "Content-Type: application/json");
        requestsMap.put("headerData", Lists.newLinkedList());
        requestsMap.put("url", "localhost,8081/api/login/phone_code?loginName=13800000000&loginValue=123456");
        requestsMap.put("queryParams", queryParamsMapList);


        queryParamsMap.put("key", "loginName");
        queryParamsMap.put("value", "13800000000");
        queryParamsMap.put("equals", true);
        queryParamsMap.put("description", "");
        queryParamsMap.put("enabled", true);


        requestsMap.put("preRequestScript", null);
        requestsMap.put("pathVariables", null);
        requestsMap.put("pathVariableData", Lists.newLinkedList());
        requestsMap.put("method", "GET");
        requestsMap.put("data", dataMapList);


        dataMap.put("key", "username");
        dataMap.put("value", "gashli");
        dataMap.put("description", "");
        dataMap.put("type", "text");
        dataMap.put("enabled", true);

        requestsMap.put("dataMode", "params");
        requestsMap.put("tests", null);
        requestsMap.put("currentHelper", "normal");
        requestsMap.put("helperAttributes", null);
        requestsMap.put("time", System.currentTimeMillis());
        requestsMap.put("name", "手机快捷登录");
        requestsMap.put("description", "");
        requestsMap.put("collectionId", "bbcf0f80-f007-46cd-574e-ca5bfaf92d2c");
        requestsMap.put("folder", "bbcf0f80-f007-46cd-574e-ca5bfaf92d2c");
        requestsMap.put("responses", Lists.newLinkedList());


        foldersMap.put("id", "1c83f451-3bfd-8aca-0d79-f7ecd579a89c");
        foldersMap.put("name", "商品");
        foldersMap.put("description", "");
        foldersMap.put("order", Lists.newLinkedList());//
        foldersMap.put("folders_order", Lists.newLinkedList());
        foldersMap.put("owner", 0);
        foldersMap.put("collectionId", "2d7e86e4-ea77-2b34-da5f-927beae854f2");
    }
*/

    public static List<String> getFolderOrder(List<Map<String, Object>> foldersMapList){
        if(CollectionUtils.isEmpty(foldersMapList)){
            return Lists.newLinkedList();
        }
        List<String> folderOrder = Lists.newLinkedList();
        for (Map<String, Object> stringObjectMap : foldersMapList) {
            String id = String.valueOf(stringObjectMap.get("id"));
            if ( StringUtils.isNotEmpty(id)){
                folderOrder.add(id);
            }
        }
        return folderOrder;
    }
    public static List<String> getRequestOrder(List<Map<String, Object>> requestsMapList){
        if(CollectionUtils.isEmpty(requestsMapList)){
            return Lists.newLinkedList();
        }
        List<String> folderOrder = Lists.newLinkedList();
        for (Map<String, Object> stringObjectMap : requestsMapList) {
            String id = String.valueOf(stringObjectMap.get("id"));
            if ( StringUtils.isNotEmpty(id)){
                folderOrder.add(id);
            }
        }
        return folderOrder;
    }
}
